package work.parabola.keyboardswitch.pojo.query.storeswitch;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.util.BigDecimalUtil;

import java.math.BigDecimal;

@Data
@ToString
@EqualsAndHashCode
public class InputQuery {

    private Integer id;

    private String amount;

    private Integer amountInt;

    private String price;

    private BigDecimal priceDecimal;

    public ServerResponse validate() {
        if (this.id == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),
                    ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        try {
            this.amountInt = Integer.parseInt(this.amount);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("数量错误");
        }
        try {
            this.priceDecimal = new BigDecimal(this.price).setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("单价错误");
        }
        if (this.amountInt.equals(0)) {
            return ServerResponse.createByErrorMessage("入库数量不能为0");
        }
        if (this.priceDecimal.equals(BigDecimalUtil.ZERO_SCALE_2)) {
            return ServerResponse.createByErrorMessage("入库单价不能为0");
        }
        return ServerResponse.createBySuccess();
    }
}
